package com.yys.zproject.controller.user;

import com.yys.zproject.entity.UserEntity;
import com.yys.zproject.service.UserService;
import com.yys.zproject.service.impl.UserServiceimpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/user/register")
public class UserRegisterController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //TODO: 用户注册逻辑
        System.out.println("用户注册功能....");
        //用户注册的数据,页面表单提交后放到请求体中带过来
        String username = req.getParameter("username");
        String email = req.getParameter("email");
        String password = req.getParameter("password");
        String confirmPwd = req.getParameter("confirm-password");

        System.out.println("用户注册数据:"+username+email+password+confirmPwd);
        //校验数据
        //验证用户名是否为 至少6位的字母和数字组合
        String usernameRegex = "^[a-zA-Z0-9]{6,}$";
        //验证用户名是否为 至少6位的字母和数字组合
        String passwordRegex = "^[a-zA-Z0-9]{6,}$";
        //
        String emailRegex = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z]{2,}$";


        if(!username.matches(usernameRegex))
        {
            gotoRegisterPage(req, resp,"输入数据不正确,请核对");
            return;
        }
        if(!password.matches(passwordRegex))
        {
            gotoRegisterPage(req, resp,"输入数据不正确,请核对");
            return;
        }
        if(!email.matches(emailRegex))
        {
            gotoRegisterPage(req, resp,"输入数据不正确,请核对");
            return;
        }

        if(!password.equals(confirmPwd))
        {
            gotoRegisterPage(req, resp,"输入数据不正确,请核对");
            return;
        }
        //把数据保存到数据库
        System.out.println("注册数据合法性校验通过...");

        //用户数据封装到javabean
        UserEntity user = new UserEntity();
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        //调用service层的方法
        UserService userService = new UserServiceimpl();
        try {
            //使用重定向
//            userService.register(user);
//            String msg = URLEncodeUtil.encode("注册成功!");
//            resp.sendRedirect("/zpro/login.jsp?msg="+msg);


            //使用转向,跳转到登录页面
//            req.setAttribute("msg","注册成功!");
//            req.getRequestDispatcher("/login.jsp").forward(req,resp);

            //给域中放数据,重定向获取(session)
            //缺点:浏览器不关闭,session一直都存在
            userService.register(user);
            HttpSession session = req.getSession();
            session.setAttribute("msg", "注册成功!");
            resp.sendRedirect("/zpro/login.jsp");


        } catch (SQLException e)
        {
            gotoRegisterPage(req, resp,"用户名和邮箱已经存在,请重新输入");

        }
        catch (Exception e)
        {
            //数据库连不上是IO异常
            gotoRegisterPage(req, resp,"当前注册用户太多,请稍后再试");
        }

    }

    private static void gotoRegisterPage(HttpServletRequest req, HttpServletResponse resp,String msg) throws ServletException, IOException {
        req.setAttribute("msg", msg);
        req.getRequestDispatcher("/register.jsp").forward(req, resp);
    }
}
